<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加用户</title>
<link href="/resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/resources/lib/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="/resources/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/resources/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/resources/lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="/resources/lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="/resources/lib/json2.js" type="text/javascript"></script>
<script src="/resources/js/jsF7.js" type="text/javascript"></script>
<script>
var form = null;
var info = '${info}';
var $grid = null;
var manager = null;
var manager2 = null;

$(function (){
    //工具条
    $("#toptoolbar").ligerToolBar({ items: [
        { text: '保存', id:'save',click: submitForm,icon: 'save' }
    ]
    });
    
	
	//创建form对象
	 form = $("#form2").ligerForm({
	      inputWidth: 170, labelWidth: 90, space: 40,
          validate: true,
          fields: [ 
              { name: "uuid", type: "hidden" },
              { name: "password", type: "hidden" },
              { label: "用户名", name: "userName", newline: false, type: "text", validate: {remote:{type:"POSt",url:"/sysUser/checkUserName",
            	  data:{uuid:function(){
            	  var uuidEd = form.getEditor("uuid"); //form中应用
                  var uuid = uuidEd.val();
            	  return uuid;
            	  }}},required: true,minlength: 1},validateMessage:{remote:"此用户名已经存在"}},
              { label: "真实姓名", name: "trueName",newline: false,  type: "text", validate: {required: true,minlength: 1 }},
              { display: "所属部门", name: "depGroup.uuid", newline: false, type: "popup",textField:'depGroup.name',validate: { required: true, minlength: 1 },
             	 editor: {
                     grid: getDepGroupGrid(true),
                     valueField:'uuid',
                     textField: 'name',
                     parms :{leaf:'1',date:new Date().getTime()},
                     width: 300,
                     height:100,
                     realTimeLoad:true
            	 }
              },
              { label: "电话", name: "phone",newline: false,  type: "text"},
              { label: "手机", name: "mobile",newline: false,  type: "text"},
              { label: "邮箱", name: "mail",newline: false,  type: "text"},
              { label: "身份证号", name: "idCard",newline: false,  type: "text"},
              { label: "职位", name: "position",newline: false,  type: "text"},
              { label: "qq号", name: "qq",newline: false,  type: "text"},
              { label: "身份证地址", name: "idAddress",newline: true,  type: "text",width: 470},
              { label: "现居地", name: "xjAddress",newline: true,  type: "text",width: 470},
              { label: "备注", name: "remark", newline: true, type: "text",width: 470}
          ]
    });
	
	  //创建表格对象
     $grid = $("#maingrid").ligerGrid({
         columns: [
         { name:'ischecked', hide:true,type:"text" },
         { name: 'uuid', hide:true,type:"text"},
         { display: '角色名', name: 'roleName', width: 100,type:"text"},
         { display: '备注', name: 'remark', width: 100,type: "text"}
         ],
         width: '100%',height: '99%',
         usePager:false,
         checkbox: true,headerRowHeight:22, rowHeight:22, rownumbers:true,
         onLoadData:onloadData,
         url:'/sysUser/listRoleData',
         isChecked: f_isChecked
     });
	 //创建组织树
 	createTree1();
	 //创建菜单树
 	createTree2();
	
	if(info != null && info != ""){
		form.setData(JSON.parse(info));
		//form.setFieldValidate("userName",{remote:{type:"POSt",url:"/sysUser/checkUserName",data:{uuid:''}},required: true,minlength: 1,},{});
	}
	if('${status}' == 1){
		$('#toptoolbar').hide();
	}
	
	//总界面
    $("#tab1").ligerTab({ 
/*     	onBeforeSelectTabItem:function(tabid){
            if(tabid == 'tabitem2'){
            	createTree1();
            }else if(tabid == 'tabitem3'){
            	createTree2();
            }
    	} */
    });
    function getUserInfo(){
   	 var uuidEd = form.getEditor("uuid"); //form中应用
   	 var uuid = uuidEd.getValue();
   	 var userNameEd = form.getEditor("userName");
   	 var userName = userNameEd.getValue();
   	 return {uuid:uuid,userName:userName};
   }
	
});


//保存
function submitForm(){
	//表单数据 
	var forms = form.getData();
	var formData=JSON.stringify(forms);
	var rows = $grid.getSelectedRows();
	var gridData = JSON.stringify(rows);
	var nodes = manager.getChecked();
	var ids = '';
	for(var i=0; i<nodes.length; i++){
		if (ids != '') ids += ',';
		ids += nodes[i].data.uuid;
	}
	
	var nodes2 = manager2.getChecked();
	var ids2 = '';
	for(var i=0; i<nodes2.length; i++){
		if (ids2 != '') ids2 += ',';
		ids2 += nodes2[i].data.uuid;
	}
	
	if (form.valid()){
		//console.log(data);
		$.ajax({
           loading: '正在保存数据中...',
           type:'post',
           url: "/sysUser/saveOrUpdateSysUser",
		   dataType:'json',
           data: {rows:gridData,form:formData,treeData:ids,treeData2:ids2},
           success: function (data) {
        	   
				$.ligerDialog.success(data.message,'信息',function(){
					form.setData(data.data);
			    });
           },
           error: function (message) {
          	 	$.ligerDialog.error("保存失败");
           }
       });
    }else{
        form.showInvalid();
    } 
}

//获取数据前添加过滤条件
function onloadData(){
	var uuid = null;
	if(info != null && info != ""){
		uuid = JSON.parse(info).uuid;
	}
	this.set('parms', [{ name: "userId", value:uuid, temp: true }]);
}

//选中事件
function f_isChecked(rowdata){
	 if (rowdata.ischecked == true) 
         return true;
     return false;
}

//创建树1
function createTree1(){
	var userId = null;
	if(info != null && info != ""){
		userId = JSON.parse(info).uuid;
	}
	//加载数据
	$.ajax({
        loading: '正在加载数据中...',
        type:'post',
        url: "/sysUser/listDepGroupData",
		dataType:'json',
        data: {userId:userId},
        success: function (data) {
        	manager = $("#tree1").ligerTree({
    		 	data:data,
    		 	checkbox: true,
    		 	isExpand: 2,
             	idFieldName :'uuid',
             	slide:false,
             	parentIDFieldName:'parentId',
             	textFieldName:'name',
             	width: '100%',
             	height: '99%'
        	 });
        },
        error: function (message) {
       	 	$.ligerDialog.error("加载数据失败");
        }
    });
}

//创建树1
function createTree2(){
	var userId = null;
	if(info != null && info != ""){
		userId = JSON.parse(info).uuid;
	}
	//加载数据
	$.ajax({
        loading: '正在加载数据中...',
        type:'post',
        url: "/sysUser/listMenuData",
		dataType:'json',
        data: {userId:userId},
        success: function (data) {
        	manager2 = $("#tree2").ligerTree({
    		 	data:data,
    		 	checkbox: true,
    		 	isExpand: 2,
             	idFieldName :'uuid',
             	slide:false,
             	parentIDFieldName:'parentId',
             	textFieldName:'name',
             	width: '100%',
             	height: '99%'
        	 });
        },
        error: function (message) {
       	 	$.ligerDialog.error("加载数据失败");
        }
    });
}



</script>
</head>
<body>
	<div id="toptoolbar"></div>
	<div style="width: 100%" >
		<form id="form2"></form>
	</div>
    <div id="tab1" style="width: 100%;height:100% ; border:1px solid #A3C0E8; "> 
       	<div title="角色信息" style="height:100%">
       		<div style="height:354px;width: 100%;overflow: scroll;">
 				<div id="maingrid"></div> 
			</div>
       	</div>
     	<div title="组织信息" style="height:354px">
 				<h4>组织树</h4>
 				<div style="height:354px;width: 100%;overflow: scroll;"> 
     				<ul id="tree1"></ul> 
 				</div> 
     	</div>
     	<div title="菜单信息" style="height:354px;">
 				<h4>菜单树</h4>
 				<div style="height:354px;width: 100%;overflow: scroll;"> 
     				<ul id="tree2"></ul> 
 				</div> 
     	</div>
    </div>
	
    <div style="display:none"></div>
</body>
</html>